package com.ruoyi.system.controller;


import com.doudian.open.api.order_batchDecrypt.OrderBatchDecryptRequest;
import com.doudian.open.api.order_batchDecrypt.OrderBatchDecryptResponse;
import com.doudian.open.api.order_batchDecrypt.param.CipherInfosItem;
import com.doudian.open.api.order_batchDecrypt.param.OrderBatchDecryptParam;
import com.doudian.open.api.order_orderDetail.OrderOrderDetailRequest;
import com.doudian.open.api.order_orderDetail.OrderOrderDetailResponse;
import com.doudian.open.api.order_orderDetail.data.ShopOrderDetail;
import com.doudian.open.api.order_orderDetail.param.OrderOrderDetailParam;
import com.doudian.open.core.AccessToken;
import com.doudian.open.core.AccessTokenBuilder;
import com.doudian.open.core.GlobalConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/hmk/douDianApi/")
public class DouDianApiController {

    @PermitAll
    @GetMapping("/decryptStr")
    public String decryptStr() {
        GlobalConfig.initAppKey("7444805882086622760");
        GlobalConfig.initAppSecret("a2ed7006-78e7-4614-b27f-c3e1080a0864");
        AccessToken accessToken = AccessTokenBuilder.build(175974257L); //入参为shopId
        GlobalConfig.initAppKey("6989147827339429420");
        GlobalConfig.initAppSecret("284371ec-c15c-489e-9ce1-673b243603c6");
        accessToken = AccessTokenBuilder.refresh(accessToken);
        log.info("获取token:{}", accessToken.getAccessToken());
        OrderOrderDetailRequest request = new OrderOrderDetailRequest();
        OrderOrderDetailParam param = request.getParam();
        param.setShopOrderId("6941008749290525904");
        OrderOrderDetailResponse response = request.execute(accessToken);
        ShopOrderDetail orderDetail = response.getData().getShopOrderDetail();
        OrderBatchDecryptRequest oBDRequest = new OrderBatchDecryptRequest();
        OrderBatchDecryptParam oBDParam = new OrderBatchDecryptParam();
        List<CipherInfosItem> cipherInfosItems = new LinkedList<>();
        CipherInfosItem cipherInfosItem1 = new CipherInfosItem();
        cipherInfosItem1.setAuthId("6941008749290525904");
        cipherInfosItem1.setCipherText(orderDetail.getEncryptPostTel());
        cipherInfosItems.add(cipherInfosItem1);
        CipherInfosItem cipherInfosItem2 = new CipherInfosItem();
        cipherInfosItem2.setAuthId("6941008749290525904");
        cipherInfosItem2.setCipherText(orderDetail.getEncryptPostReceiver());
        cipherInfosItems.add(cipherInfosItem2);
        CipherInfosItem cipherInfosItem3 = new CipherInfosItem();
        cipherInfosItem3.setAuthId("6941008749290525904");
        cipherInfosItem3.setCipherText(orderDetail.getEncryptPayTel());
        cipherInfosItems.add(cipherInfosItem3);
        oBDParam.setCipherInfos(cipherInfosItems);
        oBDRequest.setParam(oBDParam);
        OrderBatchDecryptResponse OBDResponse = oBDRequest.execute(accessToken);
        String returnStr = OBDResponse.toString();
        log.info("解密后的:{}", returnStr);
        return returnStr;
    }
}
